import config from './config.js'

// 页面跳转
export async function openPage(link, type='') {
	if (type == 'redirect') {
		uni.redirectTo({url: link})
		return
	}
	if (type == 'switchTab') {
		uni.switchTab({url: link})
		return
	}
	if (type == 'reLaunch'){
		uni.reLaunch({url: link})
		return
	}
	uni.navigateTo({url: link})
}

// 选择图片
export function getImage(num) {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: num,
			sizeType: ['compressed'],
			success: (res) => {
				resolve(res.tempFilePaths)
			}
		})
	})
}

// 选择视频
export function getVideo() {
	return new Promise((resolve, reject) => {
		uni.chooseVideo({
			compressed: true,
			success: (res) => {
				resolve(res.tempFilePath)
			}
		})
	})
}

// 上传图片/视频
export async function upload(up_tempFilePath,data) {
	uni.showLoading({title: '上传中',mask: true})
	let token = uni.getStorageSync('token')
  let tenantId = uni.getStorageSync('tenantId')?uni.getStorageSync('tenantId'):'1';//登录时默认传1，登录后拿到了tenantId值再覆盖掉
  let merId = uni.getStorageSync('merId')?uni.getStorageSync('merId'):'1';//登录时默认传1，登录后拿到了merId值再覆盖掉
	let array1 = {'Authori-zation':token,'tenant-id':tenantId,'mer-id':merId};
  
	return new Promise((resolve, rejecte) => {
		
		uni.uploadFile({
			url: config.FILE_HOST + '/api/admin/merchant/upload/image',
			filePath: up_tempFilePath,
			header: array1,
			name: 'multipart',
			formData:data,
			success(res) {
				let itemA = JSON.parse(res.data);
				let item = itemA.data.url;
			/* 	console.log(JSON.parse(res.data));
				console.log(JSON.parse(res.data).fileName);
				console.log(res.data);
				console.log(item); */
				uni.hideLoading()
				resolve(item)
			},
			fail(err) {
				uni.hideLoading()
			}
		})
	})
}

// 预览图片
export async function previewImage(url,current=0) {
	var array = url.split(';')	
	uni.previewImage({
		urls:array,
		current:current
	})
}

// 富文本图片溢出修正
export function formatRichText(html) {
	var imageReg = /<video\b.*?(?=<)/g
	let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
		match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
		match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
		match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
		return match;
	});
	newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
		match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
		return match;
	});
	newContent = newContent.replace(/<br[^>]*\/>/gi, '');
	newContent = newContent.replace(/\<img/gi,
		'<img style="max-width:100%;height:auto;display:inline-block;margin:5px auto;"');
	newContent = newContent.replace(/&lt;/g,'<').replace(/&gt;/g,'>')
	newContent = newContent.replace(/iframe/g,'video').replace('/dev-api',config.FILE_HOST).replace('</iframe>','')
	return newContent;
}

// 富文本可选颜色列表
const colorGroup = ['#FFFFFF','#EAEAEA','#D5D5D5','#C0C0C0','#ABABAB','#969696','#818181','#6C6C6C','#575757','#424242','#2D2D2D','#181818','#000000','#4D000A','#4D0E00','#4D1B00','#4D2D00','#4D3800','#2A4D00','#004D1C','#00424D','#001A4D','#000D4D','#16004D','#42004D','#4D0034','#770813','#771F06','#792E00','#774B04','#785D03','#437004','#006622','#03616C','#063078','#031A79','#27066E','#650370','#77064F','#A1151E','#A23511','#A64500','#A26D0A','#A38408','#5F940A','#008026','#07828B','#114BA3','#072CA6','#3C108F','#8A0993','#A11069','#CB272D','#CC5120','#D25F00','#CC9213','#CFAF0F','#7EB712','#009A29','#0DA5AA','#206CCF','#0E42D2','#551DB0','#B010B6','#CB1E83','#F53F3F','#F77234','#FF7D00','#F7BA1E','#FADC19','#9FDB1D','#00B42A','#14C9C9','#3491FA','#165DFF','#722ED1','#D91AD9','#F5319D','#F76560','#F99057','#FF9A2E','#F9CC45','#FBE842','#B5E241','#23C343','#37D4CF','#57A9FB','#4080FF','#8D4EDA','#E13EDB','#F754A8','#F98981','#FAAC7B','#FFB65D','#FADC6D','#FCF26B','#C9E968','#4CD263','#5EDFD6','#7BC0FC','#6AA1FF','#A871E3','#E865DF','#F979B7','#FBACA3','#FCC59F','#FFCF8B','#FCE996','#FDFA94','#DCF190','#7BE188','#89E9E0','#9FD4FD','#94BFFF','#C396ED','#F08EE6','#FB9DC7','#FDCDC5','#FDDDC3','#FFE4BA','#FDF4BF','#FEFEBE','#EDF8BB','#AFF08B','#B7F4EC','#C3E7FE','#BEDAFF','#DDBEF6','#F7BAEF','#FDC2DB','#FFECE8','#FFF3E8','#FFF7E8','#FFFCE8','#FEFFE8','#FCFFE8','#E8FFEA','#E8FFFB','#E8F7FF','#E8F3FF','#F5E8FF','#FFE8FB','#FFE8F1']

module.exports = {
	openPage: openPage,
	getImage : getImage,
	getVideo : getVideo,
	upload : upload,
	previewImage : previewImage,
	formatRichText : formatRichText,
	colorGroup : colorGroup
}